package com.h2psoft.truyen.tranh.slidingmenu;

import vn.persoft.extra.customview.SlidingFragmentActivity;
import vn.persoft.extra.customview.SlidingMenu;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.activities.MainActivity;

public class BaseActivity extends SlidingFragmentActivity implements OnItemClickListener {
	// private String tag = BaseActivity.class.getCanonicalName();

	int version = android.os.Build.VERSION.SDK_INT;
	int width;
	protected ListFragment mFrag;
	private ListView lvMenu;
	private MenuAdapter adapterMenu;
	public static SlidingMenu sm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
		// customize the SlidingMenu
		sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		sm.setFadeDegree(0.8f);
		setSlidingActionBarEnabled(true);
		initListView();
	}

	private void initListView() {
		lvMenu = (ListView) findViewById(R.id.lvMenuItem_BaseActivty);
		adapterMenu = new MenuAdapter(this);
		adapterMenu.add(new SampleItem(R.string.list_comic, R.drawable.h2psoft_list_ico));
		adapterMenu.add(new SampleItem(R.string.new_comic, R.drawable.h2psoft_new_ico));
		adapterMenu.add(new SampleItem(R.string.other_app, R.drawable.ico_setting));
		adapterMenu.add(new SampleItem(R.string.rate, R.drawable.h2psoft_like_icon));
		adapterMenu.add(new SampleItem(R.string.share, R.drawable.ico_share));
		adapterMenu.add(new SampleItem(R.string.about, R.drawable.ico_share));
		lvMenu.setAdapter(adapterMenu);
		lvMenu.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		switch (position) {
		case 0:
			MainActivity.mPager.setCurrentItem(0);
			getSlidingMenu().showContent();
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
			break;
		case 1:
			MainActivity.mPager.setCurrentItem(1);
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
			getSlidingMenu().showContent();
			break;

		}

	}

	public class MenuAdapter extends ArrayAdapter<SampleItem> {

		public MenuAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			icon.setBackgroundResource(getItem(position).iconRes);

			title.setText(getItem(position).tag);

			return convertView;
		}

	}

	private class SampleItem {
		private int tag;
		private int iconRes;

		public SampleItem(int tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
		}
	}

}
